package be.vdab.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import be.vdab.DAO.PizzaDAO;
import be.vdab.entities.Pizza;


@WebServlet("/pizzas/voorkeuren.htm")
public class VoorkeurPizzaServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final String INPUT_VIEW = "/WEB-INF/JSP/voorkeurpizzasvragen.jsp";
	private static final String SUCCES_VIEW = "/WEB-INF/JSP/voorkeurpizzastonen.jsp";
	private final PizzaDAO pizzaDAO = new PizzaDAO();


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String view;
	if (request.getParameterValues("nummer") == null) {
		request.setAttribute("pizzas", pizzaDAO.findAll());
		view = INPUT_VIEW;
	} else {
		List<Pizza> pizzas = new ArrayList<>();
		try {
			for (String nummerAlsString:request.getParameterValues("nummer")) {
				long nummer = Long.parseLong(nummerAlsString);
				Pizza pizza = pizzaDAO.read(nummer);
				if (pizza != null) {
					pizzas.add(pizza);
				}
			}
			request.setAttribute("pizzas", pizzas);
			view = SUCCES_VIEW;
		}catch (NumberFormatException ex) {
			request.setAttribute("pizzas", pizzaDAO.findAll());
			view = INPUT_VIEW;
		}
	}
	RequestDispatcher dispatcher = request.getRequestDispatcher(view);
	dispatcher.forward(request, response);
	}
}
